package routes

import (
	"douyin/controller"
	"douyin/middlewares"
	"douyin/setup/logger"
	"net/http"

	"github.com/gin-gonic/gin"
)

func SetupRouter(mode string) *gin.Engine {
	// 开发/发布环境切换
	if mode == gin.ReleaseMode {
		gin.SetMode(gin.ReleaseMode)
	}
	r := gin.New()
	// 配置中间件：日志相关，跨域，限流
	r.Use(logger.GinLogger(), logger.GinRecovery(true), middlewares.Cors())
	apiRouter := r.Group("/douyin")

	// basic apis
	apiRouter.GET("/ping", func(c *gin.Context) {
		c.JSON(http.StatusOK, gin.H{
			"msg": "pong",
		})
	})
	apiRouter.POST("/user/register/", controller.Register)
	apiRouter.POST("/user/login/", controller.Login)
	// .Use(middlewares.RateLimitMiddleware(1*time.Second, 1))// 每一秒钟添加一个令牌，接口限流
	apiRouter.GET("/feed/", controller.Feed)
	//配置JWT鉴权中间件
	apiRouter.Use(middlewares.JWTAuthMiddleware())
	{
		apiRouter.GET("/user/", controller.UserInfo)
		apiRouter.POST("/publish/action/", controller.Publish)
		apiRouter.GET("/publish/list/", controller.PublishList)

		// extra apis - I
		apiRouter.POST("/favorite/action/", controller.FavoriteAction)
		apiRouter.GET("/favorite/list/", controller.FavoriteList)
		apiRouter.POST("/comment/action/", controller.CommentAction)
		apiRouter.GET("/comment/list/", controller.CommentList)

		// extra apis - II
		apiRouter.POST("/relation/action/", controller.RelationAction)
		apiRouter.GET("/relation/follow/list/", controller.FollowList)
		apiRouter.GET("/relation/follower/list/", controller.FollowerList)
	}

	// 注册pprof相关路由
	//pprof.Register(r)
	// 404错误
	r.NoRoute(func(c *gin.Context) {
		c.JSON(http.StatusOK, gin.H{
			"msg": "404",
		})
	})
	return r
}
